package com.ruoyi.lvlian.util;

import java.io.*;
import java.util.Base64;

public class ImageToBase64 {
    public static void main(String[] args) {
        String imagePath = "http://open-mediarecoder.oss-cn-hangzhou.aliyuncs.com/CAPTURE_FILES/174b03759c28486eb22858cf912919dc/lvlian001/FW5810320-2/1744780346000.jpg?Expires=1744787547&OSSAccessKeyId=LTAI4G6HFM3XPqa8rBjxHJRE&Signature=R7cOCz5IIgJEHPNtzqxh2bej7ms%3D";
        try (InputStream imageInputStream = new FileInputStream(imagePath);
             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = imageInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }

            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);
            String dataUrl = "data:image/jpeg;base64," + base64Image;

            System.out.println("Base64 Image: ");
            System.out.println(dataUrl);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
